<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="评价内容" prop="content">
          <el-input
            v-model="form.model.content"
            autosize
            type="textarea"
            placeholder="请输入评价内容"
          />
        </el-form-item>
        <el-form-item label="评价图片">
          <sa-uploader v-model="form.model.images" :multiple="true" fileType="image"></sa-uploader>
        </el-form-item>
        <el-form-item label="评价星级" prop="level">
          <div class="levels">
            <el-rate v-model="form.model.level" />
          </div>
          <!-- :icons="['#sa-shop-star', '#sa-shop-star', '#sa-shop-star']"
            :void-icon="'#sa-shop-star'"
            :colors="['#faad14', '#faad14', '#faad14']" -->
        </el-form-item>
        <el-form-item label="评价用户" v-if="form.model.fakeUser" prop="fakeUser.id">
          <el-button
            type="primary"
            class="is-link"
            @click="selectFakeUser"
            v-if="!form.model.fakeUser.id"
          >
            选择用户
          </el-button>
          <div class="sa-flex sa-row-between sa-w-360" v-if="form.model.fakeUser.id">
            <div class="sa-flex">
              <div class="sa-m-r-60">{{ form.model.fakeUser.id }}</div>
              <div>{{ form.model.fakeUser.nickname }}</div>
            </div>
            <el-button type="danger" class="is-link sa-m-l-40" @click="deleteFakeUserItem()">
              移除
            </el-button>
          </div>
        </el-form-item>

        <el-form-item label="商品选择" prop="goods_id">

          <el-button type="primary" class="is-link" @click="selectGoods" v-if="!form.model.goods_id"
            >选择商品</el-button
          >
          <div class="sa-template-content sa-template-goods" v-if="form.model.goods_id">
              <div class="header sa-flex">
                <div class="item">商品信息</div>
                <div class="oper">操作</div>
              </div>
              <div class="list sa-flex">
                <div class="item sa-flex">
                  <sa-image :url="goods.data.image" size="40"></sa-image>
                  <div class="sa-m-l-8">
                    <div class="title">{{ goods.data.title }}</div>
                    <div class="price">¥{{ goods.data.price.join('~') || 0 }}</div>
                  </div>
                </div>
                <div class="oper">
                  <el-button class="is-link" type="danger" @click="deleteItems()"
                    >移除</el-button
                  >
                </div>
              </div>
            </div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.model.status">
            <el-radio label="normal">显示</el-radio>
            <el-radio label="hidden">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" @click="confirm">添加虚拟评价</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { reactive, ref, unref, watch } from 'vue';
  import { api } from '../goods.service';
  // import useFakeUser from './fakeUser';
  import { useModal } from '@/sheep/hooks';
  import GoodsSelect from '@/app/shop/admin/goods/goods/select.vue';
  import FakeUserSelect from '@/app/shop/admin/data/fakeUser/select.vue';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      content: '',
      images: [],
      level: 0,
      user_id: '',
      fakeUser: {
        nickname: '',
        id: '',
      },
      goods_id: '',
      status: 'normal',
    },
    rules: {
      content: [{ required: true, message: '请输入评价内容', trigger: 'blur' }],
      // images: [{ required: true, message: '请选择评价图片', trigger: 'blur' }],
      level: [{ required: true, message: '请选择评价星级', trigger: 'blur' }],
      fakeUser: {
        id: [{ required: true, message: '请选择评价用户', trigger: 'blur' }],
      },
      status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
      goods_id: [{ required: true, message: '请选择商品信息', trigger: 'blur' }],
    },
  });
  const goods = reactive({
    data: {},
  });
  const loading = ref(false);

  // const { fakeUser, deliverCompany, getDeliverCompany, onChangeExpressCode, remoteMethod } =
  //   useFakeUser();
  // watch(
  //   () => fakeUser.id,
  //   () => {
  //     form.model.fakeUser = {
  //       nickname: fakeUser.nickname,
  //       id: fakeUser.id,
  //     };
  //   },
  // );

  function selectGoods() {
    let id = '';
    useModal(
      GoodsSelect,
      {
        title: '选择商品',
        id,
      },
      {
        confirm: (res) => {
          goods.data = res.data;
          form.model.goods_id = res.data.id;
        },
      },
    );
  }
  function selectFakeUser() {
    let id = '';
    useModal(
      FakeUserSelect,
      {
        title: '选择评价用户',
        id,
      },
      {
        confirm: (res) => {
          form.model.fakeUser.nickname = res.data.nickname;
          form.model.fakeUser.id = res.data.id;
        },
      },
    );
  }
  function deleteFakeUserItem() {
    form.model.fakeUser = {};
  }
  function deleteItems() {
    goods.data = {};
    form.model.goods_id = '';
  }

  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      form.model.user_id = form.model.fakeUser.id;
      delete form.model.fakeUser;
      form.model.goods_id = goods.data.id;
      const { error } = await api.comment.add(form.model);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
</script>
<style lang="scss" scoped>
  .goods-title {
    line-height: 16px;
    // width: 160px;
  }
  :deep() {
    .el-rate .el-rate__item {
      color: var(--sa-place);
      height: 24px;
      .el-icon svg {
        width: 24px;
        height: 24px;
      }
    }
  }
</style>
